package top.scsoul.voes.examination.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import top.scsoul.voes.examination.entity.QuestionBank;
import top.scsoul.voes.examination.entity.Score;
import top.scsoul.voes.examination.mapper.ScoreMapper;
import top.scsoul.voes.examination.service.ExaminationService;
import top.scsoul.voes.examination.service.ScoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Score)表服务实现类
 *
 * @author guolei
 * @since 2021-07-26 18:56:58
 */
@Service("scoreService")
public class ScoreServiceImpl implements ScoreService {
    @Resource
    private ScoreMapper scoreMapper;

    @Autowired
    ExaminationService examinationService;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Score queryById(Integer id) {
        return this.scoreMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Score> queryAllByLimit(int offset, int limit) {
        return this.scoreMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param score 实例对象
     * @return 实例对象
     */
    @Override
    public Score insert(Score score) {
        this.scoreMapper.insert(score);
        return score;
    }

    /**
     * 修改数据
     *
     * @param score 实例对象
     * @return 实例对象
     */
    @Override
    public Score update(Score score) {
        this.scoreMapper.update(score);
        return this.queryById(score.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.scoreMapper.deleteById(id) > 0;
    }

    @Override
    public Map<String, Object> queryByUsernameAndExamId(String username, Integer examId) {
        //答题信息
        List<Score> answerList = scoreMapper.queryAnyByUsernameAndExamId(username, examId);
        //试卷
        List<QuestionBank> questionList = examinationService.testPaper(examId);
        //组装
        Map<String, Object> map = new HashMap<>();
        map.put("answerList", answerList);
        map.put("questionList", questionList);
        return map;


    }

    /**
     * 分数查询
     *
     * @param username
     * @param examId
     * @return
     */
    @Override
    public Float querySelfScore(String username, Integer examId) {
        return scoreMapper.queryScoreByUsernameAndExamId(username, examId);
    }
}